/**
 * CSGO检视图 逻辑封装
 */
$.extend({
    CSGO_INSPECT: function (opts) {
        var type = opts.type,//1.查看视图  其他：落地页重新生成          
            $this = opts.el,//检视图 DOM
            queue_timeId = null;
        count = 0;//排队轮询问 次数记录

        var inspect_url = $.base64.encode($this.data('inspect')) || '',
            inspect = $this.data('inspect') || '',
            appid = $this.data('appid') || '',
            from = $this.data('from') || '',
            item_name = $this.data('item_name') || '',
            market_hash_name = $this.data('market_hash_name') || '',
            steam_id = $this.data('steam_id') || '',
            classid = $this.data('classid') || '',
            instanceid = $this.data('instanceid') || '',
            asset_id = $this.data('asset_id') || '',
            item_id = $this.data('item_id') || '',
            retype = $this.data('retype') || '',
            url = $this.data('url') || '',
            rarity_name = $this.data('rarity_name') || '',
            key = $this.data('key'),
            ismodal = $this.data('ismodla') || '',
            data = {
                "appid": appid,
                "from": from,
                "classid": classid,
                "instanceid": instanceid,
                "asset_id": asset_id,
                "item_id": item_id,
                "steam_id": steam_id,
                "inspect_url": inspect_url,
                "rarity_name": rarity_name,
                "key": key
            } || '';

        if (type === 1) {
            getCsgoInspect(url, data)
        } else {
            $('#in-confirm-modal').modal('show');
        }

        function getCsgoInspect(url, data) {
            $this.button('loading');
            $.get(url, data).success(function (res) {
                res = JSON.parse(res);
                if (res.status === 200) {
                    if (res.data.queue_num > 0) {
                        count++;
                        var listNum = res.data.queue_num;
                        $('.cimc').html(tipText(1, listNum));
                        if (count == 1) {
                            $('#csgo-inspect-modal').modal('show');
                        }
                        queue_timeId = setTimeout(function () {
                            getCsgoInspect(url, data)
                        }, 3000);
                    } else {
                        $this.addClass('hide');
                        $this.closest('.csgo-img-bg').find('.two-inspect').removeClass('hide')
                        $this.closest('.csgo-img-bg').find('.two-inspect').attr('data-img', res.data.image)
                        $this.closest('.csgo-img-bg').find('.two-inspect').click()
                        $this.button('reset');
                    }
                } else if (res.status === 500 || res.status === 510) {
                    $this.button('reset');
                    $('.cimc').html(tipText());
                    $('#csgo-inspect-modal').modal('show');
                } else {
                    res = JSON.parse(res);
                    $this.button('reset');
                    $.showErrorMessage({"content": res.message})
                }
            })
        };


        $('#csgo-inspect-modal').off('hidden.bs.modal').on('hidden.bs.modal', function () {
            if (type == 3) {
                window.location.reload();
                return;
            }
            $this.button('reset');
            clearTimeout(queue_timeId);
        });

        //重新请求验证码
        $("#in-confirm-modal").off('shown.bs.modal').on('shown.bs.modal', function (e) {
            $('.refersh-captcha').trigger('click');
        });


        //重新生成确认
        $(document).off('click', '.inspect-confirm-btn').on('click', '.inspect-confirm-btn', function () {
            var code = $("#csgo-inspect-verify").val();
            if (code.length != 5) {
                ZENG.msgbox.show("验证码格式有误", 3000)
            } else {
                var $btn = $(this).button('loading');
                var data = {
                    classid: classid,
                    instanceid: instanceid,
                    asset_id: asset_id,
                    appid: appid,
                    from: from == 'steam_inventory' ? 'inventory' : 'sell',
                    inspect: inspect,
                    item_id: item_id,
                    steamName: item_name,
                    market_hash_name: market_hash_name,
                    rarity_name: rarity_name,
                    steam_id: steam_id,
                    code: code,
                    type: retype,
                    key: key
                };

                inspectRefresh();

                function inspectRefresh() {
                    $.get('/csgo/item/inspectRefresh', data, function (res) {
                        $btn.button('reset');
                        if (res.status === 200) {
                            if (res.data.image == '') {
                                $("#in-confirm-modal").modal('hide');
                                $('.cimc').html(tipText(1, res.data.queue_num));
                                $('#csgo-inspect-modal').modal('show');
                                // queue_timeId = setTimeout(function () {
                                //     inspectRefresh()
                                // }, 3000);
                            } else {
                                if (ismodal == 1) {
                                    $('#inspect-modal').attr('src', res.data.image)
                                    res.data.image
                                } else {
                                    window.location.reload();
                                }
                            }
                        } else {
                            $("#in-confirm-modal").modal('hide');
                            $.showMessage({'content': res.message});
                        }
                    }, 'json').error(function () {
                        $("#in-confirm-modal").modal('hide');
                        $.showErrorMessage('发生意外，请重新尝试')
                        $btn.button('reset');
                    });
                }
            }
        });

        //点击重试
        $(document).off('click', '.tryAgainIn').on('click', '.tryAgainIn', function () {
            // $('#csgo-inspect-modal').modal('hide').addClass('hide');
            // $this.trigger('click');
            getCsgoInspect(url, data);
        })

        //提示文案
        function tipText(t, p) {
            //t = 1.排队 2.查看 3.重试
            var html;
            if (t === 1) {
                // html = '重新检视中，完成将会以站内信的形式推送。';
                html = '您正处在队列中，前方还有 <span class="ft-orange wait-list">' + p + '</span> 人排队，完成将会以站内信的形式推送。'
            } else if (t === 2) {
                html = '检视图已经生成，请<a class="ft-orange" target="_blank" href=' + p + '>点击查看</a>。'
            } else {
                html = '当前饰品检视图暂时无法生成，请<a class="ft-orange tryAgainIn" href="javascript:void(0)">点击重试</a>。'
            }
            return html;

        }

    }
})